﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace Web.Shared
{
    public partial class ulogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
         
            string userName = txtUserName.Value.Trim();
            string password = txtPassword.Value.Trim();
            //如果用户名和密码为空 则返回 不做任何处理
            if (userName == "" || password == "")
            {
                lblMsg.Text = "请输入完整的用户名和密码！";
                return;
            }
            if (Membership.ValidateUser(userName, password))
            {
                MembershipUser userObj = Membership.GetUser(Context.User.Identity.Name);
                string[] roles = Roles.GetRolesForUser(userName);
                string userRoles = "";
                foreach (string item in roles)
                {
                    userRoles += item + ",";
                }
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(120), false, userRoles.TrimEnd(new char[] { ',' }), "/");
                string HashTicket = FormsAuthentication.Encrypt(Ticket);
                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
                Response.Cookies.Add(UserCookie);
                if (Context.Request["ReturnUrl"] != null)
                {
                    //Response.Redirect();
                    Response.Write("<script language = javascript>window.parent.location.href='" + Context.Request["ReturnUrl"] + "'</script>");
                }
                else
                {
                    string url = "/user/index.aspx";
                    Response.Write("<script language = javascript>window.parent.location.href='" + url + "'</script>");
                    //Response.Write("<script>window.open('" + url + "');</script>");
                }
            }
            else
            {
                lblMsg.Text = "用户名和密码不匹配.请重新输入！";
            }
        }

      
    }
}
